package com.mgface.framework.client;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.jackson.JacksonFeature;

import com.mgface.framework.client.filter.ClientResponseFilterWrapper;

public class App {
	public static void main(String[] args) throws Exception {
		
		/**
		 * Get操作
		 */
//		Client client = ClientBuilder.newClient();
//		WebTarget webTarget = client.target("http://localhost:8080/SpringJersey/rest/first").path("3");
//		webTarget = webTarget.queryParam("id", "1001");
//		Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
//		System.out.println(response.getStatus()+" "+response.readEntity(String.class));
		/**
		 * Post操作
		 */
//		Client client = ClientBuilder.newClient().register(JacksonFeature.class);
//		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first");
//		Form form = new Form();
//		form.param("id", "1001");
//		Response response = target.request(MediaType.APPLICATION_JSON).buildPost(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED)).invoke();
//		System.out.println(response.getStatus());
//		System.out.println(response.readEntity(User.class));
//		System.out.println(response.getMediaType());
//		response.close();
//		/**
//		 * Delete操作
//		 */
//		Client client = ClientBuilder.newClient();
//		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first/delete/1001");
//		Response response = target.request().delete();
//		System.out.println(response.getStatus());
//		System.out.println(response.readEntity(String.class));
//		/**
//		 * Put操作
//		 */
//		Client client = ClientBuilder.newClient();
//		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first/");
//		User u = new User("1002", "1002", "1002", new Date());
//		Response response = target.request().buildPut(Entity.entity(u, MediaType.APPLICATION_JSON)).invoke();
//		System.out.println(response.getStatus());
//		System.out.println(response.readEntity(String.class));
//		/**
//		 * 查询所有用户
//		 */
//		Client client = ClientBuilder.newClient();  
//        WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first/all");  
//        Response response = target.request().get();  
//        String value = response.readEntity(String.class);
//        System.out.println(value);
		
//		Client client = ClientBuilder.newClient();
//		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first").path("{id}").resolveTemplate("id", "1001");
//		Response response = target.request(MediaType.APPLICATION_JSON).get();
//		System.out.println(response.getStatus()+" "+response.readEntity(String.class));
		
		/*User u = new User("1003", "1003", "1003", null);
		Client client = ClientBuilder.newClient().register(JacksonFeature.class);
		Invocation.Builder build = client.target("http://localhost:8080/SpringJersey/rest/first").request(MediaType.APPLICATION_JSON);
		User u1 = build.post(Entity.entity(u, MediaType.APPLICATION_JSON), User.class);
		System.out.println(u1.getCreateTime());*/
		
		//测试异步
//		User u = new User("1003", "1003", "1003", null);
//		Client client = ClientBuilder.newClient().register(JacksonFeature.class);
//		Invocation.Builder build = client.target("http://localhost:8080/SpringJersey/rest/first").request(MediaType.APPLICATION_JSON);
//		Future<User> feature = build.async().post(Entity.entity(u, MediaType.APPLICATION_JSON), User.class);
//		System.out.println(new Date());
//		System.out.println(feature.get());
//		System.out.println(new Date());
		
//		//发起回调
//		User u = new User("1003", "1003", "1003", null);
//		Invocation.Builder builder = ClientBuilder.newClient()
//				.target("http://localhost:8080/SpringJersey/rest/first").request(MediaType.APPLICATION_JSON);
//		Future<User> feature = builder.async().post(Entity.entity(u, MediaType.APPLICATION_JSON), new InvocationCallback<User>() {
//			public void completed(User response) {
//				System.out.println("回调成功："+response.getCreateTime());
//			}
//			public void failed(Throwable throwable) {
//				System.out.println("回调失败："+throwable);
//				
//			}
//		});
//		System.out.println("哒哒哒。。。。");
//		System.out.println(feature.get());
		
		
//		//延迟调用/请求构建
//		Invocation.Builder builder = ClientBuilder.newClient().target("http://localhost:8080/SpringJersey/rest/first").request(MediaType.APPLICATION_JSON);
//		User u = new User("1003", "1003", "1003", null);
//		Invocation invocation = builder.buildPost(Entity.entity(u, MediaType.APPLICATION_JSON));
////		//同步调用
////		Response response = invocation.invoke();
////		System.out.println("---------------");
////		System.out.println(response.getStatus());
////		System.out.println(response.readEntity(String.class));
//		//异步调用
//		Future<User> future = invocation.submit(new GenericType<User>(){});
//		System.out.println("---------------");
//		System.out.println(future.get().getCreateTime());
		
		//客户端请求过滤器
//		User u = new User("1003", "1003", "1003", null);
//		Client client = ClientBuilder.newClient();
//		client.register(JacksonFeature.class);
//		client.register(ClientRequestFilterWrapper.class);
//		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first");
//		User user = target.request(MediaType.APPLICATION_JSON).post(Entity.entity(u, MediaType.APPLICATION_JSON), User.class);
//		System.out.println(user.getUserId()+"|"+user.getCreateTime());
		
		//客户端响应过滤器
		User u = new User("1003", "1003", "1003", null);
		Client client = ClientBuilder.newClient();
		client.register(JacksonFeature.class);
		client.register(ClientResponseFilterWrapper.class);
		WebTarget target = client.target("http://localhost:8080/SpringJersey/rest/first");
		User user = target.request(MediaType.APPLICATION_JSON).post(Entity.entity(u, MediaType.APPLICATION_JSON), User.class);
		System.out.println(user.getUserId()+"|"+user.getCreateTime());
	}
	
}
